<?php
/**
 * Created by PhpStorm.
 * User: 刘帅
 * Date: 2019-06-04
 * Time: 20:32
 */

namespace Extend\Drive\Cache;

class Memcached
{
    private static $handle = null;

    /**
     * @初始化连接1
     * @return \Memcached|null
     */
    public static function init()
    {
        $option = array(
            'host' => config('cache.memcached.servers.host') ?? '127.0.0.1',
            'port' => config('cache.memcached.servers.port') ?? 11211,
        );

        if (!self::$handle) {
            self::$handle = new \Memcached();
            self::$handle->addServer($option['host'], $option['port']);
        }

        return self::$handle;
    }

    /**
     * @存
     * @param $key
     * @param $value
     * @param int $exptime
     * @return bool
     */
    public static function set($key, $value, $exptime = 0)
    {
        if (!self::init()) return false;
        $res = self::init()->set($key, $value, $exptime);
        return $res;
    }

    /**
     * @不存在存储
     * @param $key
     * @param $value
     * @param int $exptime
     * @return bool
     */
    public static function add($key, $value, $exptime = 0)
    {
        if (!self::init()) return false;
        $res = self::init()->add($key, $value, $exptime);
        return $res;
    }

    /**
     * @存在则替换
     * @param $key
     * @param $value
     * @param int $exptime
     * @return bool
     */
    public static function replace($key, $value, $exptime = 0)
    {
        if (!self::init()) return false;
        self::add($key,0);
        $res = self::init()->replace($key, $value, $exptime);
        return $res;
    }

    /**
     * @存在则在后面添加
     * @param $key
     * @param $value
     * @return bool
     */
    public static function append($key, $value)
    {
        if (!self::init()) return false;
        self::add($key,0);
        $res = self::init()->append($key, $value);
        return $res;
    }

    /**
     * @存在则在前面添加
     * @param $key
     * @param $value
     * @return bool
     */
    public static function prepend($key, $value)
    {
        if (!self::init()) return false;
        self::add($key,0);
        $res = self::init()->prepend($key, $value);
        return $res;
    }

    /**
     * @取
     * @param $key
     * @return mixed
     */
    public static function get($key)
    {
        $func = is_array($key) ? 'getMulti' : 'get';
        return self::init()->{$func}($key);
    }

    /**
     * @删除
     * @param $key
     * @return bool
     */
    public static function delete($key)
    {
        if (!self::init()) return false;
        $res = self::init()->delete($key);
        return $res;
    }

    /**
     * @递增
     * @param $key
     * @param $number
     * @return bool
     */
    public static function increment($key,$number)
    {
        if (!self::init()) return false;
        self::add($key,0);
        if(!is_int($number)) return false;
        $res = self::increment($key,$number);
        return $res;
    }

    /**
     * @递减
     * @param $key
     * @param $number
     * @return bool
     */
    public static function decrement($key,$number)
    {
        if (!self::init()) return false;
        self::add($key,0);
        if(!is_int($number)) return false;
        $res = self::decrement($key,$number);
        return $res;
    }
}